import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import Home from '../views/home.vue';
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/dashboard',
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                meta: {
                    title: '系统首页',
                    permiss: '1',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/home/dashboard.vue'),
            },
            {
                path: '/sample/form',
                name: 'receipt',
                meta: {
                    title: '样品上传',
                    permiss: '1',
                },
                component: () => import(/* webpackChunkName: "receipt" */ '../views/sample/upload/sample_form.vue'),
            },
            {
                path: '/sample/manage',
                name: 'sample_manage',
                meta: {
                    keepAlive: true,
                    title: '库存管理',
                    permiss: '2',
                },
                component: () => import(/* webpackChunkName: "sample_manage" */ '../views/sample/sample_manage.vue'),
            },
            /* */
            {
                path: '/project/current',
                name: 'project_current',
                meta: {
                    title: '现有项目&样品',
                    permiss: '2',
                },
                component: () => import(/* webpackChunkName: "project_current" */ '../views/project/project_sample_form.vue'),
            },
            {
                path: '/user/add',
                name: 'user_add',
                meta: {
                    title: '用户管理',
                },
                component: () => import(/* webpackChunkName: "register" */ '../views/user/useradd.vue'),
            },
            {
                path: '/user/record',
                name: 'user_rec',
                meta: {
                    title: '用户记录',
                },
                component: () => import(/* webpackChunkName: "register" */ '../views/user/userRecord.vue'),
            },
        ],
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录',
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/auth/login.vue'),
    },
  
    {
        path: '/403',
        name: '403',
        meta: {
            title: '没有权限',
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/error/403.vue'),
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();
    const role = localStorage.getItem('ms_username');
    const permiss = usePermissStore();
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permiss && !permiss.key.includes(to.meta.permiss)) {
        // 如果没有权限，则进入403
        next('/403');
    } else {
        next();
    }
});

router.afterEach(() => {
    NProgress.done()
})

export default router;
